using System;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Text;
using JsonFx.Json;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.GZip;

public class CommonUtils
{

		public static bool compare (byte[] from, byte[] to)
		{
				int length = from.Length;
				if (length == to.Length) {
						for (length--; length>= 0; length--) {
								if (from [length] != to [length]) {
										return false;
								}
						}

						return true;
				}

				return false;
		}

		public static void clone (Object obj, Object clone)
		{
				FieldInfo[] fieldInfos = obj.GetType ().GetFields ();
				foreach (FieldInfo fieldInfo in fieldInfos) {
						try {
								fieldInfo.SetValue (clone, fieldInfo.GetValue (obj));
				
						} catch {
				
						}
				}
		}

		public static byte[] jsonWrite (object value)
		{
				if (value == null) {
						return null;
				}

				MemoryStream stream = new MemoryStream ();
				using (JsonWriter jsonWriter = new JsonWriter (stream)) {
						jsonWriter.Write (value);
				}

				return stream.ToArray ();
		}

		public static T jsonRead<T> (Stream stream)
		{
		JsonReader jsonReader = new JsonReader (stream);
			return jsonReader.Deserialize<T> ();
		}
		
		public static T jsonRead<T> (byte[] buffer, int offset, int count)
		{
				return jsonRead<T> (new MemoryStream (buffer, offset, count, false));
		}

		public static byte[] readStream (Stream stream)
		{
				MemoryStream outBuffer = new MemoryStream ();
				byte[] block = new byte[1024];
				int bytesRead;
				while ((bytesRead = stream.Read (block, 0, block.Length)) > 0) {
						outBuffer.Write (block, 0, bytesRead);
				}

				return outBuffer.ToArray ();
		}

		public static void zipCompress (byte[] buffer, int offset, int count, Stream outStream)
		{
				GZipOutputStream gZipStream = new GZipOutputStream (outStream);
				gZipStream.Write (buffer, offset, count);
				gZipStream.Close ();
		}

		public static byte[] zipCompress (byte[] buffer, int offset, int count)
		{
				MemoryStream stream = new MemoryStream ();
				zipCompress (buffer, offset, count, stream);
				return stream.ToArray ();
		}

		public static string zipDeCompressString (byte[] data, int offset, int count)
		{
				MemoryStream stream = new MemoryStream (data, offset, count, false);
				GZipInputStream gZipStream = new GZipInputStream (stream);
				try {
						return Encoding.UTF8.GetString (readStream (gZipStream));

				} finally {
						gZipStream.Close ();
				}
		}

		public static byte[] zipCompressValue (object data)
		{
		MemoryStream outStream = new MemoryStream ();
		using(JsonWriter jsonWriter = new JsonWriter (outStream)){
			jsonWriter.Write(data);
		}

				MemoryStream stream = new MemoryStream ();
				GZipOutputStream gZipStream = new GZipOutputStream (stream);
		byte[] buffer = outStream.ToArray();
		gZipStream.Write(buffer, 0, buffer.Length);
				gZipStream.Close ();
				return stream.ToArray ();
		}
		
		public static T zipDeCompressValue<T> (byte[] data, int offset, int count)
		{
				if (typeof(T) == typeof(string)) {
						return (T)(object)zipDeCompressString (data, offset, count);
				}

				MemoryStream stream = new MemoryStream (data, offset, count, false);
				GZipInputStream gZipStream = new GZipInputStream (stream);
				try {		
						return jsonRead<T> (gZipStream);
			
				} finally {
						gZipStream.Close ();
				}
		}
}
